package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class UserGroup {
	// DB
    ResultSet rs = null;
	
    SQLiteDBClass myDb;
    SQLiteDatabase db;
    Context context;
    
    private static final String TABLE_FFUSERGROUP = "_FFUSERGROUP";
    
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
	
	private String OID,
			UsrOpt,
			IsActive,
			IsDeleted,
			UserGroupCode,
			UserGroupName;
	
    public UserGroup() {UserGroupCode = UserGroupName = "";
    }
    
    
    public UserGroup( 
    		String OID,
    		String UsrOpt,
    		String IsActive,
    		String IsDeleted,
    		String UserGroupCode,
    		String UserGroupName
    		)
    {
    	this.OID = OID;
    	this.UsrOpt = UsrOpt;
    	this.IsActive = IsActive;
    	this.IsDeleted = IsDeleted;
    	this.UserGroupCode = UserGroupCode;
    	this.UserGroupName = UserGroupName;
    }
    
    public String getOID() {
		return OID;
	}

	public void setOID(String oID) {
		OID = oID;
	}

	public String getUsrOpt() {
		return UsrOpt;
	}

	public void setUsrOpt(String usrOpt) {
		UsrOpt = usrOpt;
	}

	public String getIsActive() {
		return IsActive;
	}

	public void setIsActive(String isActive) {
		IsActive = isActive;
	}

	public String getIsDeleted() {
		return IsDeleted;
	}

	public void setIsDeleted(String isDeleted) {
		IsDeleted = isDeleted;
	}

	public void setUserGroupCode(String UserGroupCode)
	{
		this.UserGroupCode = UserGroupCode;
	}
	public void setUserGroupName(String UserGroupName)
	{
		this.UserGroupName = UserGroupName;
	}
	
	public String getUserGroupCode()
	{
		return this.UserGroupCode;
	}
	public String getUserGroupName()
	{
		return this.UserGroupName;
	}
	
	public ResultSet selectUserGroup(String StrUserGroupName ) throws Exception {
		Connect con = new Connect(); 
		String  StrSql     = "SELECT [OID] "+						 
				   				",[LastModified] "+
				   				",[ModifyDate] "+
				   				",[ModifyTime] "+
				   				",[ModifiedBy] "+
				   				",[UserModify] "+
				   				",[CreatedDate] "+
				   				",[CreateDate] "+
				   				",[CreateTime] "+
				   				",[CreatedBy] "+
				   				",[UserCreate] "+
				   				",[IsActive] "+
				   				",[IsDeleted] "+
				   				",[UserGroupCode] "+
				   				",[UserGroupName] "+
			 			  	"FROM [dbo].[udfFFUserGroup]() "+
			 			  	"WHERE [IsDeleted]='0' ";
			     	
		if (!StrUserGroupName.equals("")) {
            StrSql += "AND [UserGroupName] LIKE '%" + StrUserGroupName + "%' ";
        }
    	
		return con.selectData(StrSql);
	 }
	
	public ResultSet defineUserGroup() throws Exception {
		Connect con = new Connect(); 
		String StrSql = "SELECT [UserGroupCode]"+
							  ",[UserGroupName] "+	

						"FROM [dbo].[udfFFUserGroup] ()" ;
		StrSql += "ORDER BY [UserGroupCode] ASC";
		
		return con.selectData(StrSql);
		}
//	 public void closeConnecttion() throws Exception {
//    	 if (conn != null) {
//             conn.close();
//         }
//    }
	 
	// ---- SQLite -------//
	 
	 public void Define_FFUserGroup() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();
			
			try {
				rs = selectUserGroup("");
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("FFUSERGROUPOID", rs.getString("OID"));
					Val.put("UserGroupCode", rs.getString("UserGroupCode"));
					Val.put("UserGroupName", rs.getString("UserGroupName"));
					db.insert(TABLE_FFUSERGROUP, null, Val);
				}
				rs.close();
			} catch (Exception e) {
				Log.d("TABLE COMPANY", "Create Table not Successfully.");
			}
			Log.d("TABLE COMPANY", "Create Table Successfully.");
			
			
		}
	 
	 public String Select_FFUserGroupCode(String FFUserGroupOID) throws Exception{
			// TODO Auto-generated method stub
			try {
				String RetrunName = "";
				
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();

				String strSQL = "SELECT  FFUSERGROUPOID As _id , * FROM "
						+ TABLE_FFUSERGROUP + " WHERE [FFUSERGROUPOID] LIKE '"
						+ FFUserGroupOID + "' ";
				Cursor cursor = db.rawQuery(strSQL, null);

				if (cursor != null) {
					if (cursor.moveToFirst()) {
						RetrunName = cursor.getString(2);
					}
				}
				cursor.close();
				db.close();
				return RetrunName;

			} catch (Exception e) {
				return "0";
			}

		}

		public String Select_FFUserGroupName(String FFUserGroupNameOID) throws Exception{
			// TODO Auto-generated method stub
			try {
				String RetrunName = "";

				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();

				String strSQL = "SELECT  FFUSERGROUPOID As _id , * FROM "
						+ TABLE_FFUSERGROUP + " WHERE [FFUSERGROUPOID] LIKE '"
						+ FFUserGroupNameOID + "' ";
				Cursor cursor = db.rawQuery(strSQL, null);

				if (cursor != null) {
					if (cursor.moveToFirst()) {
						RetrunName = cursor.getString(3);
					}
				}
				cursor.close();
				db.close();
				return RetrunName;

			} catch (Exception e) {
				return null;
			}

		}
		
		public String[][] selectAll_FFUserGroup() throws Exception{
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				
				String strSQL = "SELECT  FFUSERGROUPOID As _id , * FROM "
						+ TABLE_FFUSERGROUP;
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}
		
	 
}
